package com.xnyzc.lhy.resource.controller.jpush;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.resource.config.JPushConfigKey;
import com.xnyzc.lhy.resource.entity.jpush.BizResult;
import com.xnyzc.lhy.resource.entity.jpush.MessageEntity;
import com.xnyzc.lhy.resource.entity.jpush.OaJpushConfig;
import com.xnyzc.lhy.resource.entity.jpush.PushInfoEntity;
import com.xnyzc.lhy.resource.entity.jpush.ResultCode;
import com.xnyzc.lhy.resource.mapper.jpush.OaJpushConfigMapper;
import com.xnyzc.lhy.resource.service.jpush.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 极光推送
 * </p>
 *
 * @author liuju
 * @since 2019-09-20
 */
@Slf4j
@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private OaJpushConfigMapper oaJpushConfigMapper;

    @Value("${global.jPush.passengerAppKey}")
    private String passengerAppKey;

    @Value("${global.jPush.passengerMasterSecret}")
    private String passengerMasterSecret;

    @Value("${global.jPush.driverAppKey}")
    private String driverAppKey;

    @Value("${global.jPush.driverMasterSecret}")
    private String driverMasterSecret;


    @PostMapping("/push/message")
    public BizResult pushMessage(@RequestBody PushInfoEntity pushInfoEntity) {

        log.info("推送信息服务开始");
        String clientId = pushInfoEntity.getClientId();
        String tag = pushInfoEntity.getTag();
        if (CheckUtil.strIsEmpty(clientId)) {
            log.info("获取clientId失败");
            return BizResult.failure(ResultCode.REQ_NOT_COMPLETE);
        }

        if (CheckUtil.objIsEmpty(pushInfoEntity.getPhones())) {
            log.info("获取推送别名失败");
            return BizResult.failure(ResultCode.REQ_NOT_COMPLETE);
        }

        MessageEntity messageEntity = new MessageEntity();
        if (Objects.equals(clientId, JPushConfigKey.PASSENGER)) {
            if (CheckUtil.strIsEmpty(passengerAppKey, passengerMasterSecret)) {
                log.info("请求缺少司机端appKey信息");
                return BizResult.failure(ResultCode.REQ_NOT_COMPLETE);
            }
            // 拼接别名
            List<String> aliasList = new ArrayList<>();
            for (String phone : pushInfoEntity.getPhones()) {
                aliasList.add(JPushConfigKey.PASSENGER + phone);
            }
            messageEntity.setAlias(aliasList);
            messageEntity.setAppKey(passengerAppKey);
            messageEntity.setMasterSecret(passengerMasterSecret);
        } else if (Objects.equals(clientId, JPushConfigKey.DRIVER) || Objects.equals(clientId, JPushConfigKey.MANAGER)) {
            if (CheckUtil.strIsEmpty(driverAppKey, driverMasterSecret)) {
                log.info("请求缺少司机端appKey信息");
                return BizResult.failure(ResultCode.REQ_NOT_COMPLETE);
            }
            // 拼接别名
            List<String> aliasList = new ArrayList<>();
            for (String phone : pushInfoEntity.getPhones()) {
                aliasList.add(JPushConfigKey.DRIVER + phone);
            }
            messageEntity.setAlias(aliasList);
            messageEntity.setAppKey(driverAppKey);
            messageEntity.setMasterSecret(driverMasterSecret);
        } else {
            log.info("clientId非法");
            return BizResult.failure(ResultCode.REQ_NOT_COMPLETE);
        }

        // 获取推送信息
        QueryWrapper<OaJpushConfig> oaJpushConfigQueryWrapper = new QueryWrapper<>();
        oaJpushConfigQueryWrapper.eq(OaJpushConfig.PUSH_CODE, pushInfoEntity.getPushCode());
        List<OaJpushConfig> oaJpushConfigList = oaJpushConfigMapper.selectList(oaJpushConfigQueryWrapper);
        if (CheckUtil.collectionIsEmpty(oaJpushConfigList) || oaJpushConfigList.size() != 1) {
            log.info("获取推送信息失败");
            return BizResult.failure(ResultCode.REQ_NOT_COMPLETE);
        }

        OaJpushConfig oaJpushConfig = oaJpushConfigList.get(0);
        // 通知类型 0:通知 1：自定义消息
        messageEntity.setJPushType(0);
        // 设置推送信息
        messageEntity.setTitle(oaJpushConfig.getTitle());
        messageEntity.setAlert(oaJpushConfig.getTitle());
        JSONObject extrasJSON = pushInfoEntity.getExtras();
        extrasJSON.put("sound", oaJpushConfig.getSoundKey());
        extrasJSON.put("jumpKey", oaJpushConfig.getJumpKey());
        if(CheckUtil.strIsNotEmpty(pushInfoEntity.getJudgeCms()) && pushInfoEntity.getJudgeCms().equals("CMS")){
            messageEntity.setTitle(pushInfoEntity.getTitle());
            messageEntity.setAlert(pushInfoEntity.getContent());
            extrasJSON.put("pushLink",pushInfoEntity.getPushLink());
        }else{
            extrasJSON.put("pushLink","");
        }
        messageEntity.setExtras(extrasJSON);
        messageEntity.setSound(oaJpushConfig.getSoundKey());
        messageEntity.setMsg_content(oaJpushConfig.getContent());
        if (CheckUtil.strIsNotEmpty(pushInfoEntity.getContent())) {
            messageEntity.setMsg_content(pushInfoEntity.getContent());
        }
        log.info("请求数据：" + messageEntity.toString());
        if (StringUtils.isEmpty(messageEntity.getMsg_content())) {
            log.info("请求缺少Msg_content的值");
            return BizResult.failure(ResultCode.REQ_NOT_COMPLETE);
        }
        BizResult bizResult = messageService.sendMessage(messageEntity, clientId, tag);
        log.info("推送信息服务结束");
        return bizResult;
    }

}
